package com.yanqu.road.queue;

import com.alibaba.fastjson.JSONArray;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author duli
 * @version 1.0
 * @date 2020/9/9 11:15
 * @Description 日志分析接收队列
 */
public class LogAnalysisReceiveQueue{
    private final static Logger log = LogManager.getLogger(LogAnalysisReceiveQueue.class.getName());

    // 消息队列
    private static Queue<Object> msgQueue =new LinkedList<Object>();

    public static void add(Object msg) {
        synchronized (msgQueue) {
            msgQueue.add(msg);
        }
    }

    public static JSONArray poll(int max_num) {
        synchronized (msgQueue) {
            JSONArray jsonArray =new JSONArray();
            if(max_num==0){
                max_num=msgQueue.size();
            }
            for (int i = 0; i < max_num; i++) {
                Object data = msgQueue.poll();
                if (data == null) {
                    break;
                }
                jsonArray.add(data);
            }
            log.info("LogAnalysisReceiveQueue poll size:"+jsonArray.size());
            return jsonArray;
        }
    }

}

